package com.mine.distinctionteam.ui.home.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mine.distinctionteam.R;
import com.mine.distinctionteam.bean.home.ContentsBean;
import com.mine.distinctionteam.ui.good.GoodsActivity;

import java.util.List;
/**
 *
 */
public class TimeActivityViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<ContentsBean> list;
    private LayoutInflater inflater;
    public TimeActivityViewPagerAdapter(Context mContext , List<ContentsBean> list ){
        this.mContext=mContext;
        this.list=list;
        inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        View view=inflater.inflate(R.layout.item_fragment_home_time_activity,container,false);
        ImageView imageView_item_fragment_time_activity= (ImageView) view.findViewById(R.id.imageView_item_fragment_time_activity);
        TextView textView_item_fragment_time_activity= (TextView) view.findViewById(R.id.textView_item_fragment_time_activity);
        Glide.with(mContext).load(list.get(position).getImage()).into(imageView_item_fragment_time_activity);
//        Date curDate = new Date(System.currentTimeMillis());
//        if (curDate>=)
//        if ((System.currentTimeMillis()>=list.get(position).getTimeLimit().getBegin())&&(System.currentTimeMillis()<=list.get(position).getTimeLimit().getEnd())){
//            textView_item_fragment_time_activity.setVisibility(View.GONE);
//        }else{
//            textView_item_fragment_time_activity.setVisibility(View.VISIBLE);
//        }
        imageView_item_fragment_time_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, GoodsActivity.class);
                intent.putExtra("id", "" + list.get(position).getAction().getGoodsId());
                mContext.startActivity(intent);
            }
        });
        textView_item_fragment_time_activity.setVisibility(View.VISIBLE);
        container.addView(view);
        return view;
    }
    @Override
    //销毁item
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    //刷新GrilView数据
    public void reloadGridView(List<ContentsBean> _list, boolean isClear) {
        if (isClear) {
            list.clear();
        }
        list.addAll(_list);
        //刷新
        notifyDataSetChanged();
    }
    //Gson解析
    public ContentsBean parseJsonToList(String json) {
        //创建gson对象
        Gson gson = new Gson();
        //第一个参数 json字符串
        //第二个参数 HotBean对象
        //格式就是这么写的 有问题问gson工程师去
        return gson.fromJson(json, new TypeToken<ContentsBean>() {
        }.getType());
    }
}
